/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package todomanager.View;

/**
 *
 * @author Gi-Hoon
 */
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import javax.swing.event.*;
import javax.swing.table.TableModel;

import todomanager.MODEL.TodoManagerModel;

public class MainTable extends JPanel implements TableModelListener {

	private boolean DEBUG = false;
	private TodoManagerModel todoModel;
	private MainTableModel tableModel;
	
	private Object[][] data;
	private JTable table;
	private ResourceBundle language;

	public MainTable(TodoManagerModel todoModel, ResourceBundle lang) {
		super(new GridLayout(1, 0));

		this.language = lang;
		this.todoModel = todoModel;

		tableModel = new MainTableModel() {

			public boolean isCellEditable(int rowIndex, int mColIndex) {

				if (mColIndex == 0)
					return true;
				else
					return false;
			}
		};
		table = new JTable(tableModel);

		/*
		 * table = new JTable(tableModel){public Component
		 * prepareRenderer(TableCellRenderer renderer, int row, int column){
		 * Component c = super.prepareRenderer(renderer, row, column); Boolean b
		 * = (Boolean)getModel().getValueAt(row, 0); c.setBackground(b?
		 * Color.GREEN : null); table.repaint(); return c; } };
		 */
		// table = new JTable(tableModel);
		/*
		 * {public Component prepareRenderer(TableCellRenderer renderer, int
		 * row, int column){ Component c = super.prepareRenderer(renderer, row,
		 * column); //c.setBackground(row%2==0? Color.lightGray : null);
		 * table.repaint(); return c; } };
		 */

		// TODO Sorting is set to false since it causes array out of bounds when
		// deleting a task.
		table.setAutoCreateRowSorter(false);

		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane);
		// table.getModel().addTableModelListener(this);

		tableModel.setData();

	}

	public int getSelectedRow() {
		return table.getSelectedRow();
	}

	public int getKey(int row) {
		row = table.convertRowIndexToModel(row);
		return (Integer) tableModel.getValue(row, 7);
	}

	public JTable getTable() {
		return table;
	}

	public void refreshTable() {
		tableModel.setData();
		table.repaint();
	}

	public void tableChanged(TableModelEvent tme) {

	}
	
	public String getLabel(String labelName) {
		return this.language.getString(labelName);
	}
	
	class MainTableModel extends AbstractTableModel {
		
		private String[] columnNames = { getLabel("DoneLabel"), getLabel("NameLabel"), getLabel("DescriptionLabel"),
				getLabel("DateLabel"), getLabel("CategoryLabel"), getLabel("PriorityLabel"), getLabel("ProgressLabel") };

		private Object[][] data = todoModel.getTodoData(language);

		public int getColumnCount() {
			return columnNames.length;
		}

		public int getRowCount() {
			return data.length;
		}

		public String getColumnName(int col) {
			return columnNames[col];
		}

		public Object getValue(int row, int col) {
			return data[row][col];
		}

		public Class getColumnClass(int c) {
			return getValue(0, c).getClass();
		}

		public void setValueAt(Object value, int row, int col) {
			if (DEBUG) {
				System.out.println("Setting value at " + row + "," + col
						+ " to " + value + " (an instance of "
						+ value.getClass() + ")");
			}

			data[row][col] = value;

			if (DEBUG) {
				System.out.println("New value of data:");
				printDebugData();
			}
		}

		public void setData() {
			data = (todoModel.getTodoData(language));
		}

		private void printDebugData() {
			int numRows = getRowCount();
			int numCols = getColumnCount();

			for (int i = 0; i < numRows; i++) {
				System.out.print("    row " + i + ":");
				for (int j = 0; j < numCols; j++) {
					System.out.print("  " + data[i][j]);
				}
				System.out.println();
			}
			System.out.println("--------------------------");
		}

		/**
		 * Listening for Data Changes
		 * 
		 * @param e
		 */
		public Object getValueAt(int rowIndex, int columnIndex) {
			return data[rowIndex][columnIndex];
		}

		public void tableChanged(TableModelEvent e) {
			System.out.println("The table has changed!");
			int row = e.getFirstRow();
			int column = e.getColumn();
			TableModel model = (TableModel) e.getSource();
			String columnName = model.getColumnName(column);
			// Object data = model.getValueAt(row, column);
			// data = todoModel.getTodoData();
		}

	}

	

	

	public String getName() {
		return (String) tableModel.getValueAt(this.getSelectedRow(), 1);
	}
}
